package huawei.checkandset;

import java.util.LinkedList;
import java.util.Queue;

/**
 * https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-shu-liang-by-leetcode/
 */
public class J200 {
    public static void main(String[] args) {
        System.out.println(numIslands(new char[][]{
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}
        }));
        System.out.println(numIslands2(new char[][]{
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}
        }));
    }

    public static int numIslands(char[][] grid) {
        // dfs处理
        int count = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    bfs(grid, i, j);
                }
            }
        }
        return count;
    }

    public static void bfs(char[][] grid, int i, int j) {
        if (i < 0 || i >= grid.length) {
            return;
        }
        if (j < 0 || j >= grid[i].length) {
            return;
        }
        if (grid[i][j] == '0') {
            return;
        }
        grid[i][j] = '0';
        bfs(grid, i + 1, j);
        bfs(grid, i - 1, j);
        bfs(grid, i, j - 1);
        bfs(grid, i, j + 1);
    }

    public static int numIslands2(char[][] grid) {
        Fun2 fun2 = new Fun2(grid);
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] != '1') {
                    continue;
                }
                if (i - 1 >= 0 && grid[i - 1][j] == '1') {
                    fun2.merge(i * grid[i].length + j, (i - 1) * grid[i].length + j);
                }
                if (i + 1 < grid.length && grid[i + 1][j] == '1') {
                    fun2.merge(i * grid[i].length + j, (i + 1) * grid[i].length + j);
                }
                if (j - 1 >= 0 && grid[i][j - 1] == '1') {
                    fun2.merge(i * grid[i].length + j, i * grid[i].length + j - 1);
                }
                if (j + 1 < grid[i].length && grid[i][j + 1] == '1') {
                    fun2.merge(i * grid[i].length + j, i * grid[i].length + j + 1);
                }
            }
        }
        fun2.show();
        return fun2.count;
    }
}

class Fun2 {
    int[] parents;
    int[] ranks;
    int count;

    Fun2(char[][] grid) {
        parents = new int[grid.length * grid[0].length];
        ranks = new int[grid.length * grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    parents[i * grid[0].length + j] = i * grid[0].length + j;
                    count++;
                }else {
                    parents[i * grid[0].length + j]=-1;
                }
            }
        }
    }

    void show() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < parents.length; i++) {
            if (parents[i] == i) {
                // 顶点\
                append(parents,i,builder);
                builder.append('\n');
            }
        }
        System.out.println(builder.toString());
    }

    private void append(int[] parents, int i, StringBuilder builder) {
        builder.append('(');
        builder.append(i).append(", ");
        for (int j = 0; j < parents.length; j++) {
            if(j!=i&&parents[j]==i){
                append(parents,j,builder);
            }
        }
        builder.append(')');
    }


    int find(int index) {
        if (parents[index] == index) {
            return index;
        }
        return find(parents[index]);
    }

    void merge(int a, int b) {
        int rootA = find(a);
        int rootB = find(b);
        if (rootA == rootB) {
            return;
        }
        if (ranks[rootA] > ranks[rootB]) {
            parents[rootB] = rootA;
        } else {
            parents[rootA] = rootB;
        }
        // 合并和,联通区间-1
        count--;
    }
}